package top.lixunda.ecommerce.server.good.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import top.lixunda.commerce.common.core.entity.MessageObject;
import top.lixunda.common.validate.XumValid;
import top.lixunda.ecommerce.server.business.BaseController;
import top.lixunda.ecommerce.server.good.api.entity.db.FrequentlyQuestionDO;
import top.lixunda.ecommerce.server.good.api.entity.dto.QuestionChangeDTO;
import top.lixunda.ecommerce.server.good.api.entity.dto.QuestionInsertDTO;
import top.lixunda.ecommerce.server.good.api.entity.vo.QuestionVO;
import top.lixunda.ecommerce.server.good.api.controller.IFrequentlyQuestionController;
import top.lixunda.ecommerce.server.good.service.IFrequentlyQuestionService;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * Feign客户端，接口调用
 * FQA 常问问题数据表，目前由商家进行设置
 * </p>
 *
 * @author Xumda
 * @version time:2020/1/9 14:09
 */
@Slf4j
@RestController
public class FrequentlyQuestionController extends BaseController
        implements IFrequentlyQuestionController {

    private final IFrequentlyQuestionService frequentlyQuestionService;

    @Autowired
    public FrequentlyQuestionController(IFrequentlyQuestionService frequentlyQuestionService) {
        this.frequentlyQuestionService = frequentlyQuestionService;
    }

    /**
     * 批量插入FQA
     *
     * @param questionInsertDtoList FQA数据
     * @return 插入成功的数量
     */
    @Override
    public MessageObject<Integer> saveFqas(@XumValid List<QuestionInsertDTO> questionInsertDtoList) {
        int count = frequentlyQuestionService.saveFqas(questionInsertDtoList);
        return getSuccessMessageObject("保存数据成功", count);
    }

    /**
     * 根据商品id查询所有FQA数据
     *
     * @param id 商品id
     * @return 该商品可以展示的FQA数据，不一定是商品特有的，也可能是店铺共有的
     */
    @Override
    public MessageObject<List<QuestionVO>> queryAllFqaByGoodId(Integer id) {
        List<FrequentlyQuestionDO> list = frequentlyQuestionService.queryAllFqaByGoodIds(Collections.singletonList(id));
        List<QuestionVO> resultList = QuestionVO.fromFrequentlyQuestionDO(list);
        return getSuccessMessageObject("获取数据成功", resultList);
    }

    /**
     * 根据商品id列表查询所有FQA数据
     *
     * @param ids 商品id列表
     * @return 该商品可以展示的FQA数据，不一定是商品特有的，也可能是店铺共有的
     */
    @Override
    public MessageObject<List<QuestionVO>> queryAllFqaByGoodIds(List<Integer> ids) {
        List<FrequentlyQuestionDO> list = frequentlyQuestionService.queryAllFqaByGoodIds(ids);
        List<QuestionVO> resultList = QuestionVO.fromFrequentlyQuestionDO(list);
        return getSuccessMessageObject("获取数据成功", resultList);
    }

    /**
     * 根据店铺id查询所有FQA数据
     *
     * @param id 店铺id
     * @return 该店铺中的所有FQA数据
     */
    @Override
    public MessageObject<List<QuestionVO>> queryAllFqaByStoreId(Integer id) {
        List<FrequentlyQuestionDO> list = frequentlyQuestionService.queryAllFqaByStoreIds(Collections.singletonList(id));
        List<QuestionVO> resultList = QuestionVO.fromFrequentlyQuestionDO(list);
        return getSuccessMessageObject("获取数据成功", resultList);
    }

    /**
     * 根据店铺id列表查询所有FQA数据
     *
     * @param ids 店铺id列表
     * @return 该店铺中的所有FQA数据
     */
    @Override
    public MessageObject<List<QuestionVO>> queryAllFqaByStoreIds(List<Integer> ids) {
        List<FrequentlyQuestionDO> list = frequentlyQuestionService.queryAllFqaByStoreIds(ids);
        List<QuestionVO> resultList = QuestionVO.fromFrequentlyQuestionDO(list);
        return getSuccessMessageObject("获取数据成功", resultList);
    }

    /**
     * 根据id查询单条FQA数据
     *
     * @param id id
     * @return FQA数据
     */
    @Override
    public MessageObject<QuestionVO> queryFqaById(Integer id) {
        List<FrequentlyQuestionDO> list = frequentlyQuestionService.queryFqaByIds(Collections.singletonList(id));
        List<QuestionVO> resultList = QuestionVO.fromFrequentlyQuestionDO(list);
        return getSuccessMessageObject("获取数据成功",
                resultList.size() > 0 ? resultList.get(0) : null);
    }

    /**
     * 根据id列表查询所有FQA数据
     *
     * @param ids id列表
     * @return 该id列表包含的所有FQA数据
     */
    @Override
    public MessageObject<List<QuestionVO>> queryFqaByIds(List<Integer> ids) {
        List<FrequentlyQuestionDO> list = frequentlyQuestionService.queryFqaByIds(ids);
        List<QuestionVO> resultList = QuestionVO.fromFrequentlyQuestionDO(list);
        return getSuccessMessageObject("获取数据成功", resultList);
    }

    /**
     * 根据FQA的id更新FQA数据
     *
     * @param questionChangeDTO 跟新FQA的DTO
     * @return 更改之后的DTO数据
     */
    @Override
    public MessageObject<QuestionVO> changeQuestionById(@XumValid QuestionChangeDTO questionChangeDTO) {
        FrequentlyQuestionDO frequentlyQuestionDO = frequentlyQuestionService.changeQuestionById(questionChangeDTO);
        QuestionVO result = QuestionVO.fromFrequentlyQuestionDO(frequentlyQuestionDO);
        return getSuccessMessageObject("修改数据成功", result);
    }

    /**
     * 根据id删除FQA数据
     *
     * @param ids FQA的唯一id列表
     * @return 返回删除成功的数量
     */
    @Override
    public MessageObject<Integer> removeQuestionByIds(List<Integer> ids) {
        int count = frequentlyQuestionService.removeQuestionById(ids);
        return getSuccessMessageObject("删除数据成功", count);
    }
}
